package com.eightworld3.supermarket.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eightworld3.supermarket.domain.WareInfo;
import com.eightworld3.supermarket.domain.WarePrice;
import com.eightworld3.supermarket.mapper.WareInfoMapper;
import com.eightworld3.supermarket.mapper.WarePriceMapper;
import com.eightworld3.supermarket.service.AssetsEnum;
import com.eightworld3.supermarket.service.WarePriceService;
import com.eightworld3.supermarket.service.dto.WarePriceDTO;
import com.eightworld3.supermarket.service.dto.add.WarePriceAddDTO;
import com.eightworld3.supermarket.service.dto.result.ResponseResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author EightWorld3
 */
@Service
public class WarePriceServiceImpl extends ServiceImpl<WarePriceMapper,WarePrice> implements WarePriceService {
    private final WarePriceMapper warePriceMapper;
    private final WareInfoMapper wareInfoMapper;

    /**
     * WarePriceServiceImpl构造方法
     * @param warePriceMapper 商品价格Mapper
     * @param wareInfoMapper 商品信息Mapper
     */
    public WarePriceServiceImpl(WarePriceMapper warePriceMapper, WareInfoMapper wareInfoMapper){
        this.warePriceMapper = warePriceMapper;
        this.wareInfoMapper = wareInfoMapper;
    }

    /**
     * 查询所有
     * @return 返回所有WarePrice(商品价格)
     */
    @Override
    public List<WarePrice> selectAll(){
        return warePriceMapper.selectList(null);
    }

    /**
     * 根据warePriceId查询WarePrice
     * @param warePriceId ID号
     * @return 返回warePriceId对应的WarePrice对象(经ResponseResult包装类修饰)
     */
    @Override
    public ResponseResult selectByWarePriceId(Integer warePriceId){
        if (warePriceId == null) {
            return new ResponseResult(2, "null");
        }

        WarePrice entity = warePriceMapper.selectById(warePriceId);
        if (entity == null) {
            return new ResponseResult(2, "没有该对象");
        }

        return new ResponseResult(1,"成功",entity);
    }

    /**
     * 新增WarePrice
     * @param warePriceAddDTO WarePrice新增DTO对象
     * @return 返回当前当次新增状况(经ResponseResult包装类修饰)
     */
    @Override
    public ResponseResult addWarePrice(WarePriceAddDTO warePriceAddDTO){
        WarePrice entity = new WarePrice();

        // 商品存在判断
        WareInfo wareInfoEntity = wareInfoMapper.selectById(warePriceAddDTO.getWareId());
        if (wareInfoEntity == null){
                return new ResponseResult(2, "商品选择错误");
        }else {
            entity.setWareId(warePriceAddDTO.getWareId());
        }

        // 商品价格大于零判定
        if(warePriceAddDTO.getWarePrice() <= AssetsEnum.BOUNDARY_VALUE_ZERO.value()){
            return new ResponseResult(2, "商品价格需大于0");
        }else {
            entity.setWarePrice(warePriceAddDTO.getWarePrice());
        }

        // 商品折扣范围限制
    if (warePriceAddDTO.getWareDiscount() < AssetsEnum.BOUNDARY_VALUE_ZERO.value() || warePriceAddDTO.getWareDiscount() > AssetsEnum.MAX_DISCOUNT_LIMIT.value()){
            return new ResponseResult(2, "折扣范围为0~10");
        }else {
            entity.setWareDiscount(warePriceAddDTO.getWareDiscount());
        }

        // 备注内容字符限制
        if(warePriceAddDTO.getRemark().getBytes().length > AssetsEnum.REMARK_VARCHAR_LIMIT.value()){
            return new ResponseResult(2, "备注内容过多，需 <= 120Byte");
        }else {
            entity.setRemark(warePriceAddDTO.getRemark());
        }

        return new ResponseResult(1,"成功",warePriceMapper.insert(entity));
    }

    /**
     * 更新WarePrice
     * @param warePriceDTO WarePrice DTO对象
     * @return 返回当前当次更新状况(经ResponseResult包装类修饰)
     */
    @Override
    public ResponseResult updateWarePrice(WarePriceDTO warePriceDTO){
        WarePrice entity = new WarePrice();

        // 修改对象存在判断
        WarePrice existEntity  = warePriceMapper.selectById(warePriceDTO.getWarePriceId());
        if(existEntity == null){
            return new ResponseResult(2, "商品价格对象不存在");
        }else {
            entity.setWarePriceId(warePriceDTO.getWarePriceId());
        }

        // 商品存在判断
        WareInfo wareInfoEntity = wareInfoMapper.selectById(warePriceDTO.getWareId());
        if (wareInfoEntity == null) {
            return new ResponseResult(2, "商品选择错误");
        }else{
            entity.setWareId(warePriceDTO.getWareId());
        }


        // 商品价格大于零判定
        if(warePriceDTO.getWarePrice() <= AssetsEnum.BOUNDARY_VALUE_ZERO.value()){
            return new ResponseResult(2, "商品价格需大于0");
        }else {
            entity.setWarePrice(warePriceDTO.getWarePrice());
        }

        // 商品折扣范围限制
        if (warePriceDTO.getWareDiscount() < AssetsEnum.BOUNDARY_VALUE_ZERO.value() || warePriceDTO.getWareDiscount() > AssetsEnum.MAX_DISCOUNT_LIMIT.value()){
            return new ResponseResult(2, "折扣范围为0~10");
        }else {
            entity.setWareDiscount(warePriceDTO.getWareDiscount());
        }

        // 备注内容字符限制
        if(warePriceDTO.getRemark().getBytes().length > AssetsEnum.REMARK_VARCHAR_LIMIT.value()){
            return new ResponseResult(2, "备注内容过多，需 <= 120Byte");
        }else {
            entity.setRemark(warePriceDTO.getRemark());
        }

        return new ResponseResult(1,"成功",warePriceMapper.updateById(entity));
    }

    /**
     * 删除WarePrice
     * @param warePriceId ID号
     * @return 返回当前当次删除状况(经ResponseResult包装类修饰)
     */
    @Override
    public ResponseResult deleteWarePriceById(int warePriceId){
        int deleteNum= warePriceMapper.deleteById(warePriceId);
        if (deleteNum == 0) {
            return new ResponseResult(0, "对象不存在");
        }
        return new ResponseResult(1, "成功");
    }
}
